/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package exif_to_kml;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.GpsDirectory;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

/**
 *
 * @author offsprey14@gmail.com
 */
public class EXIF_Reader {
private String[][] data = new String[0][3];
private boolean[] hasGpsData= new boolean[0];

static double rounder = 1000000.0;










    private void addData(String filename,String location, String extra,boolean hasGps)
    {
        String[][] tmp = new String[data.length+1][3];
        boolean[] tBool = new boolean[data.length+1];
        int i =0;
        for (String[] e:data)
        {
            tBool[i] = hasGpsData[i];
            tmp[i] = data[i];
            i++;
        }
        tBool[i] = hasGps;
        tmp[i][0] = filename;
        tmp[i][1] = location;
        tmp[i][2] = extra;
        data = tmp;

        hasGpsData = tBool;
    }
    public boolean hasGpsInfo(int row)
    {
       return hasGpsData[row];
    }
    public Object[][] getData()
    {
        return data;
    }
    public String[] readEXIF(File file)
    {
         try {
                File imageFile = file;
                String extraData="";
                Metadata metadata = ImageMetadataReader.readMetadata(imageFile);

                GpsDirectory d =metadata.getDirectory(GpsDirectory.class);
                String rLat,rLon;
                boolean bLat = true,bLon  = true;
                Collection<Tag> cTags = d.getTags();
                for (Tag t:cTags)
                {
                    extraData += t.getTagName() + " : " + t.getDescription() + "\n";
                }
                rLat = d.getString(GpsDirectory.TAG_GPS_LATITUDE_REF);
                rLon = d.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF);
                String lat,lon;
                lat = d.getString(GpsDirectory.TAG_GPS_LATITUDE);
                lon = d.getString(GpsDirectory.TAG_GPS_LONGITUDE);

                if (rLat.equalsIgnoreCase("S"))
                    bLat = false;
                if (rLon.equalsIgnoreCase("W"))
                    bLon = false;
                String[] output = new String[2];
                output[0] = rationalToDD(lat,bLat) + " , " +  rationalToDD(lon,bLon);
                output[1] = extraData;
                addData(file.getName(),output[0],output[1],true);

                return output;


            } catch (ImageProcessingException ex) {
                addData(file.getName(),"No GPS Data Found",ex.getMessage(),false);
            } catch (IOException ex) {
                addData(file.getName(),"No GPS Data Found",ex.getMessage(),false);
            }

         return null;

    }
    public String rationalToDD(String rationalCoord, boolean pos)
    {
        String[] p = rationalCoord.split(" ");
        String output="";

        String[] div = p[0].split("/");
        double D = Double.valueOf(div[0]);

        div = p[1].split("/");
        double dM = Double.valueOf(div[0]) / 60;

        div = p[2].split("/");
        double dS = Double.valueOf(div[0]) / Double.valueOf(div[1]);
        
        dS = dS / 3600;
        double tD = D + dM + dS;
            tD = tD * rounder;
         long expD = Math.round(tD);


         Double out = (double)expD;
         out = out / rounder;
         String outStr = out.toString();
         if (!pos)
             outStr = "-" + outStr;
         return outStr;
    }
}
